<template>
  <el-dialog title="新增员工" :visible.sync="dialogFormVisible" @close="close">
    <el-form ref="form" :model="form" :rules="rules">
      <el-form-item prop="username" label="姓名" :label-width="formLabelWidth">
        <el-input v-model="form.username" tabindex="1" autocomplete="off" />
      </el-form-item>
      <el-form-item prop="mobile" label="手机" :label-width="formLabelWidth">
        <el-input v-model="form.mobile" tabindex="2" autocomplete="off" />
      </el-form-item>
      <el-form-item
        prop="timeOfEntry"
        label="入职时间"
        :label-width="formLabelWidth"
      >
        <el-date-picker
          v-model="form.timeOfEntry"
          value-format="yyyy-MM-dd"
          tabindex="3"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item
        prop="formOfEmployment"
        label="聘用形式"
        :label-width="formLabelWidth"
      >
        <el-select
          v-model="form.formOfEmployment"
          tabindex="4"
          placeholder="请选择活动区域"
          @change="$refs.form.validateField('formOfEmployment')"
        >
          <el-option
            v-for="item in enumObj.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        prop="workNumber"
        label="工号"
        tabindex="5"
        :label-width="formLabelWidth"
      >
        <el-input v-model="form.workNumber" autocomplete="off" />
      </el-form-item>
      <el-form-item
        prop="departmentName"
        label="部门"
        :label-width="formLabelWidth"
      >
        <el-input
          v-model="form.departmentName"
          tabindex="6"
          autocomplete="off"
          readonly
          @focus="getDept"
          @blur="onBlur"
        />
        <!-- tree -->
        <el-tree
          v-if="treeShow"
          :data="treeData"
          default-expand-all
          :props="defaultProps"
          @node-click="handleNodeClick"
        >
          <template v-slot="{ data }">
            <button
              class="treeBtn"
              style="outline:none;border:none;background-color:transparent;"
              @blur="onBlur"
            >
              {{ data.name }}
            </button>
          </template>
        </el-tree>
      </el-form-item>
      <el-form-item
        prop="correctionTime"
        value-format="yyyy-MM-dd"
        label="转正时间"
        :label-width="formLabelWidth"
      >
        <el-date-picker
          v-model="form.correctionTime"
          tabindex="7"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">{{ $t('table.cancel') }}</el-button>
      <el-button
        type="primary"
        @click="save"
      >{{ $t('table.confirm') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
// 导入枚举库
import enumObj from '@/utils/constant/employees'
// 导入接口
import { deptListAPI } from '@/api/department'
import { array2Tree } from '@/utils'
import { addEmpAPI } from '@/api/employees'
export default {
  data() {
    return {
      treeShow: false,
      dialogFormVisible: false,
      enumObj: enumObj,
      form: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [{ required: true, trigger: 'blur', message: '请输入姓名' }],
        mobile: [{ required: true, trigger: 'blur', message: '请输入手机号' }],
        formOfEmployment: [
          { required: true, trigger: 'blur', message: '请选择聘用形式' }
        ],
        workNumber: [
          { required: true, trigger: 'blur', message: '请输入工号' }
        ],
        departmentName: [
          { required: true, trigger: 'blur', message: '请选择部门' }
        ],
        timeOfEntry: [
          { required: true, trigger: 'blur', message: '请选择入职时间' }
        ],
        correctionTime: [
          { required: true, trigger: 'blur', message: '请选择转正' }
        ]
      },
      formLabelWidth: '100px',
      treeData: [],
      defaultProps: {
        // 找子节点
        children: 'children',
        // label设置显示的属性名
        label: 'name'
      }
    }
  },

  methods: {
    // 获取部门数据
    async getDept() {
      const res = await deptListAPI()
      // 转树
      this.treeData = array2Tree(res.data.depts, '')
      // 显示出tree
      this.treeShow = true
    },
    // 部门失焦
    onBlur(e) {
      // 判断我失去焦点的目标去哪
      // 如果是去的tree就先不要隐藏
      if (
        e.relatedTarget === null ||
        (!e.relatedTarget.classList.contains('el-tree-node') &&
          !e.relatedTarget.classList.contains('treeBtn'))
      ) {
        this.treeShow = false
      }
    },
    // tree节点的点击事件
    handleNodeClick(obj) {
      // 把输入的内容显示输入框
      this.form.departmentName = obj.name
      // 赋完值隐藏
      this.treeShow = false
      // 点击事件里直接清除校验结果
      this.$refs.form.clearValidate('departmentName')
    },

    async save() {
      // 1. 整个表单校验 -- 得到一个promise对
      try {
        await this.$refs.form.validate()
        // 能往下走代表通过了-发请求
        await addEmpAPI(this.form)
        console.log(this.form)
        this.$message.success('新增成功')
        this.dialogFormVisible = false
        // 刷新父组件
        this.$emit('add')
      } catch {
        console.log('没有通过')
      }
    },

    close() {
      this.form = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      // 清空校验规则
      this.$refs.form.clearValidate()
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep {
  span.el-tree-node__expand-icon {
    display: none;
  }
}
</style>
